﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using VAdvantage.Classes;


namespace VAdvantage.Grids
{
    public class DateConverter : IValueConverter, IDisposable
    {

        int _displayType = DisplayType.Date;

        public DateConverter(int dType)
        {
            _displayType = dType;
        }

        private bool _isADColumn = true;

        //public DateConverter(bool isADColumn)
        //{
        //    // TODO: Complete member initialization
        //    _isADColumn = isADColumn;
        //}

        public DateConverter(bool isADColumn, int dtype)
        {
            // TODO: Complete member initialization
            _isADColumn = isADColumn;
            _displayType = dtype;
        }


        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (_isADColumn)
            {
                if (value == null || value.ToString() == "")
                {
                    return "";
                }
                if (_displayType == DisplayType.Date)
                {
                    return String.Format("{0:d}", value);
                }
                else if (_displayType == DisplayType.DateTime)
                {
                    return String.Format("{0:g}", value);
                }
                else
                {
                    return String.Format("{0:t}", value);
                }
            }
            else
            {
                if (value == null || value.ToString() == "")
                {
                    return "";
                }
                DateTime dt;
                try
                {
                     dt = DateTime.Parse(value.ToString());
                }
                catch
                {
                    return value;
                }

                
                if (_displayType == DisplayType.Date)
                {
                    return String.Format("{0:d}", dt);
                }
                else if (_displayType == DisplayType.DateTime)
                {
                    return String.Format("{0:g}", dt);
                }
                else
                {
                    return String.Format("{0:t}", dt);
                }
                
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            //throw new NotImplementedException();
        }
    }
}
